package com.mathquiz;

import java.util.*;

public class Generator {
    private static final Random R = new Random();
    private static final int MIN = 1;
    private static final int MAX = 99;

    /* 公开接口：mode 取值范围
       add,sub,mul,div,mix2,mixmd,mix3 */
    public static List<Question> generate(int count, String mode) {
        List<Question> list = new ArrayList<>(count);
        while (list.size() < count) {
            Question q = switch (mode) {
                case "add" -> add();
                case "sub" -> sub();
                case "mul" -> mul();
                case "div" -> div();
                case "mix2" -> R.nextBoolean() ? add() : sub();
                case "mixmd" -> R.nextBoolean() ? mul() : div();
                case "mix3" -> mix3();
                default -> mix3();
            };
            if (q != null) list.add(q);
        }
        return list;
    }

    /* 纯加 */
    private static Question add() {
        int a = rand();
        int b = R.nextInt(MAX - a) + MIN;
        return new Question(a + "+" + b, a + b);
    }

    /* 纯减 */
    private static Question sub() {
        int a = rand();
        int b = R.nextInt(a - MIN) + MIN;
        return new Question(a + "-" + b, a - b);
    }

    /* 纯乘 */
    private static Question mul() {
        int a = R.nextInt(9) + MIN;          // 控制积 <100
        int b = R.nextInt(MAX / a) + MIN;
        return new Question(a + "×" + b, a * b);
    }

    /* 纯除：结果为正整数，被除数<100 */
    private static Question div() {
        int b = R.nextInt(9) + MIN;
        int res = R.nextInt(MAX / b) + MIN;
        int a = res * b;
        return new Question(a + "÷" + b, res);
    }

    /* 三个数混合：只生成 a op1 b op2 c 形式，中间及最终结果∈(0,100) */
    private static Question mix3() {
        for (int i = 0; i < 1000; i++) {     // 防死循环
            int a = rand(), b = rand(), c = rand();
            int op1 = R.nextInt(4), op2 = R.nextInt(4);

            Integer mid = calc(a, b, op1);
            if (mid == null || mid <= 0 || mid >= 100) continue;
            Integer fin = calc(mid, c, op2);
            if (fin == null || fin <= 0 || fin >= 100) continue;

            String exp = "" + a + opChar(op1) + b + opChar(op2) + c;
            return new Question(exp, fin);
        }
        return null;
    }

    /* 工具：计算一次二元运算，非法返回 null */
    private static Integer calc(int x, int y, int op) {
        return switch (op) {
            case 0 -> x + y;
            case 1 -> x - y;
            case 2 -> x * y;
            case 3 -> (y == 0 || x % y != 0) ? null : x / y;
            default -> null;
        };
    }

    private static char opChar(int op) {
        return switch (op) {
            case 0 -> '+';
            case 1 -> '-';
            case 2 -> '×';
            case 3 -> '÷';
            default -> '?';
        };
    }

    private static int rand() {
        return R.nextInt(MAX - MIN) + MIN;
    }
}